# coding: utf-8
import json


class User(object):
    """

    """
    def __init__(self, username='', password='', balance=0, shopping=[]):
        self.username = username
        self.password = password
        self.list_shopping = shopping
        self.balance = balance
        # with open('user.json') as f:
        #    self.user_list = json.loads(f.read())

    def create_user(self):
        """
        将用户信息写入到json文件中
        :return: None
        """
        # 将json文件读到内存中并decode
        with open('user.json') as f:
            user_list = json.loads(f.read())
        # 将用户基础信息拼接成列表
        list_user_info = [self.username, self.password, self.balance, self.list_shopping]
        # 向总列表里追加用户信息
        user_list.append(list_user_info)
        # encode
        json_user_info = json.dumps(user_list)
        # 写回到json文件中
        with open('user.json', 'w') as f:
            f.write(json_user_info)

    def update_balance(self, cost, num=1):
        """
        扣款
        :return: None
        """
        with open('user.json') as f:
            user_list = json.loads(f.read())

        # 根据用户名更新用户信息
        for i in user_list:
            if i.count(self.username) != 0:
                if int(i[2]) - int(cost) > 0:
                    i[2] = i[2] - int(cost) * int(num)
                    json_user_info = json.dumps(user_list)
                    # 写回到json文件中
                    with open('user.json', 'w') as f:
                        f.write(json_user_info)
                    return True
                else:
                    return False


    def recharge(self, money):
        """
        充值
        :return: None
        """
        with open('user.json') as f:
            user_list = json.loads(f.read())

        # 根据用户名更新用户信息
        for i in user_list:
            if i.count(self.username) != 0:
                i[2] = i[2] + int(money)
        pass

        # encode
        json_user_info = json.dumps(user_list)
        # 写回到json文件中
        with open('user.json', 'w') as f:
            f.write(json_user_info)

        return self.get_user_info()

    def update_shop_list(self, goods_info):
        """
        更新购物车列表
        :return:
        """
        with open('user.json') as f:
            user_list = json.loads(f.read())

        # 根据用户名更新用户信息
        for i in user_list:
            if i.count(self.username) != 0:
                i.append(goods_info)
        pass

        # encode
        json_user_info = json.dumps(user_list)
        # 写回到json文件中
        with open('user.json', 'w') as f:
            f.write(json_user_info)
            f.flush()

    def get_user_info(self):
        """
        返回用户余额
        :return: balance
        """
        with open('user.json') as f:
            user_list = json.loads(f.read())

        # 根据用户名搜索用户相关信息
        for i in user_list:
            if i.count(self.username) != 0:
                return i[2]
            else:
                continue

    def get_user_pwd(self):
        """
        返回用户密码
        :return: password
        """
        with open('user.json') as f:
            user_list = json.loads(f.read())

        # 根据用户名搜索用户相关信息
        for i in user_list:
            if i.count(self.username) != 0:
                return i[1]
            else:
                continue

    def find_user(self):
        """
        查找json文件中有没有该用户的信息
        :return: True or False
        """
        with open('user.json') as f:
            user_list = json.loads(f.read())

        for i in user_list:
            if i.count(self.username) != 0:
                return True
            else:
                continue
        return False

    def check_shopping_history(self):
        """
        查看历史交易信息(含本次已经发生的交易)
        :return:
        """
        with open('user.json') as f:
            user_list = json.loads(f.read())

        list_ret = []
        # 根据用户名搜索用户相关信息
        for i in user_list:
            if i.count(self.username) != 0:
                for j in range(3, len(i)-3):
                    list_ret.append(i[j])
                return list_ret
            else:
                continue


if __name__ == '__main__':
    # list_first_menu()
    # print "---"
    # list_second_menu(2)
    # user = User('ps', 'p@s$w0rd', 6378345, ['20160516', 'iPhone', 1])
    # user.create_user()
    # user.update_info()
    # print user.get_user_info()
    # print user.find_user()
    # user = User('ps','p@s$w0rd')
    # print user.check_shopping_history()
    # print user.get_user_pwd()
    pass
